// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.znet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.DirectoryStream.Filter;
import java.util.ArrayList;
import zombie.Lua.LuaEventManager;
import zombie.debug.DebugLog;
import zombie.network.GameServer;

public class SteamWorkshop implements ISteamWorkshopCallback {
    public static final SteamWorkshop instance = new SteamWorkshop();
    private ArrayList<SteamWorkshopItem> stagedItems = new ArrayList();
    private ArrayList<ISteamWorkshopCallback> callbacks = new ArrayList();

    public static void init() {
        if (SteamUtils.isSteamModeEnabled()) {
            instance.n_Init();
        }

        if (!GameServer.bServer) {
            instance.initWorkshopFolder();
        }
    }

    public static void shutdown() {
        if (SteamUtils.isSteamModeEnabled()) {
            instance.n_Shutdown();
        }
    }

    private void copyFile(File file0, File file1) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file0);

            try {
                FileOutputStream fileOutputStream = new FileOutputStream(file1);

                try {
                    fileOutputStream.getChannel().transferFrom(fileInputStream.getChannel(), 0L, file0.length());
                } catch (Throwable throwable0) {
                    try {
                        fileOutputStream.close();
                    } catch (Throwable throwable1) {
                        throwable0.addSuppressed(throwable1);
                    }

                    throw throwable0;
                }

                fileOutputStream.close();
            } catch (Throwable throwable2) {
                try {
                    fileInputStream.close();
                } catch (Throwable throwable3) {
                    throwable2.addSuppressed(throwable3);
                }

                throw throwable2;
            }

            fileInputStream.close();
        } catch (IOException iOException) {
            iOException.printStackTrace();
        }
    }

    private void copyFileOrFolder(File file0, File file1) {
        if (file0.isDirectory()) {
            if (!file1.mkdirs()) {
                return;
            }

            String[] string = file0.list();

            for (int _int = 0; _int < string.length; _int++) {
                this.copyFileOrFolder(new File(file0, string[_int]), new File(file1, string[_int]));
            }
        } else {
            this.copyFile(file0, file1);
        }
    }

    private void initWorkshopFolder() {
        File file0 = new File(this.getWorkshopFolder());
        if (file0.exists() || file0.mkdirs()) {
            File file1 = new File("Workshop" + File.separator + "ModTemplate");
            File file2 = new File(this.getWorkshopFolder() + File.separator + "ModTemplate");
            if (file1.exists() && !file2.exists()) {
                this.copyFileOrFolder(file1, file2);
            }
        }
    }

    public ArrayList<SteamWorkshopItem> loadStagedItems() {
        this.stagedItems.clear();

        for (String string : this.getStageFolders()) {
            SteamWorkshopItem steamWorkshopItem = new SteamWorkshopItem(string);
            steamWorkshopItem.readWorkshopTxt();
            this.stagedItems.add(steamWorkshopItem);
        }

        return this.stagedItems;
    }

    public String getWorkshopFolder() {
        return zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + "Workshop";
    }

    public ArrayList<String> getStageFolders() {
        ArrayList arrayList = new ArrayList();
        Path path0 = FileSystems.getDefault().getPath(this.getWorkshopFolder());

        try {
            if (!Files.isDirectory(path0, new LinkOption[0])) {
                Files.createDirectories(path0);
            }
        } catch (IOException iOException) {
            iOException.printStackTrace();
            return arrayList;
        }

        Filter filter = new Filter<Path>() {
            public boolean accept(Path path) throws IOException {
                return Files.isDirectory(path, new LinkOption[0]);
            }
        };

        try {
            DirectoryStream directoryStream = Files.newDirectoryStream(path0, filter);

            try {
                for (Path path1 : directoryStream) {
                    String string = path1.toAbsolutePath().toString();
                    arrayList.add(string);
                }
            } catch (Throwable throwable0) {
                if (directoryStream != null) {
                    try {
                        directoryStream.close();
                    } catch (Throwable throwable1) {
                        throwable0.addSuppressed(throwable1);
                    }
                }

                throw throwable0;
            }

            if (directoryStream != null) {
                directoryStream.close();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return arrayList;
    }

    public boolean CreateWorkshopItem(SteamWorkshopItem item) {
        if (item.getID() != null) {
            throw new RuntimeException("can't recreate an existing item");
        } else {
            return this.n_CreateItem();
        }
    }

    public boolean SubmitWorkshopItem(SteamWorkshopItem item) {
        if (item.getID() != null && SteamUtils.isValidSteamID(item.getID())) {
            long _long = SteamUtils.convertStringToSteamID(item.getID());
            if (!this.n_StartItemUpdate(_long)) {
                return false;
            } else if (!this.n_SetItemTitle(item.getTitle())) {
                return false;
            } else if (!this.n_SetItemDescription(item.getSubmitDescription())) {
                return false;
            } else {
                int _int = item.getVisibilityInteger();
                if ("Mod Template".equals(item.getTitle())) {
                    _int = 2;
                }

                if (!this.n_SetItemVisibility(_int)) {
                    return false;
                } else {
                    if (!this.n_SetItemTags(item.getSubmitTags())) {
                    }

                    if (!this.n_SetItemContent(item.getContentFolder())) {
                        return false;
                    } else {
                        return !this.n_SetItemPreview(item.getPreviewImage()) ? false : this.n_SubmitItemUpdate(item.getChangeNote());
                    }
                }
            }
        } else {
            throw new RuntimeException("workshop ID is required");
        }
    }

    public boolean GetItemUpdateProgress(long[] _long) {
        return this.n_GetItemUpdateProgress(_long);
    }

    public String[] GetInstalledItemFolders() {
        return GameServer.bServer ? GameServer.WorkshopInstallFolders : this.n_GetInstalledItemFolders();
    }

    public long GetItemState(long itemID) {
        return this.n_GetItemState(itemID);
    }

    public String GetItemInstallFolder(long itemID) {
        return this.n_GetItemInstallFolder(itemID);
    }

    public long GetItemInstallTimeStamp(long itemID) {
        return this.n_GetItemInstallTimeStamp(itemID);
    }

    public boolean SubscribeItem(long itemID, ISteamWorkshopCallback callback) {
        if (!this.callbacks.contains(callback)) {
            this.callbacks.add(callback);
        }

        return this.n_SubscribeItem(itemID);
    }

    public boolean DownloadItem(long itemID, boolean bHighPriority, ISteamWorkshopCallback callback) {
        if (!this.callbacks.contains(callback)) {
            this.callbacks.add(callback);
        }

        return this.n_DownloadItem(itemID, bHighPriority);
    }

    public boolean GetItemDownloadInfo(long long0, long[] long1) {
        return this.n_GetItemDownloadInfo(long0, long1);
    }

    public long CreateQueryUGCDetailsRequest(long[] _long, ISteamWorkshopCallback iSteamWorkshopCallback) {
        if (!this.callbacks.contains(iSteamWorkshopCallback)) {
            this.callbacks.add(iSteamWorkshopCallback);
        }

        return this.n_CreateQueryUGCDetailsRequest(_long);
    }

    public SteamUGCDetails GetQueryUGCResult(long handle, int index) {
        return this.n_GetQueryUGCResult(handle, index);
    }

    public long[] GetQueryUGCChildren(long _long, int _int) {
        return this.n_GetQueryUGCChildren(_long, _int);
    }

    public boolean ReleaseQueryUGCRequest(long handle) {
        return this.n_ReleaseQueryUGCRequest(handle);
    }

    public void RemoveCallback(ISteamWorkshopCallback callback) {
        this.callbacks.remove(callback);
    }

    public String getIDFromItemInstallFolder(String dir) {
        if (dir != null && dir.replace("\\", "/").contains("/workshop/content/108600/")) {
            File file = new File(dir);
            String string = file.getName();
            if (SteamUtils.isValidSteamID(string)) {
                return string;
            }

            DebugLog.log("ERROR: " + string + " isn't a valid workshop item ID");
        }

        return null;
    }

    private native void n_Init();

    private native void n_Shutdown();

    private native boolean n_CreateItem();

    private native boolean n_StartItemUpdate(long var1);

    private native boolean n_SetItemTitle(String var1);

    private native boolean n_SetItemDescription(String var1);

    private native boolean n_SetItemVisibility(int var1);

    private native boolean n_SetItemTags(String[] var1);

    private native boolean n_SetItemContent(String var1);

    private native boolean n_SetItemPreview(String var1);

    private native boolean n_SubmitItemUpdate(String var1);

    private native boolean n_GetItemUpdateProgress(long[] var1);

    private native String[] n_GetInstalledItemFolders();

    private native long n_GetItemState(long var1);

    private native boolean n_SubscribeItem(long var1);

    private native boolean n_DownloadItem(long var1, boolean var3);

    private native String n_GetItemInstallFolder(long var1);

    private native long n_GetItemInstallTimeStamp(long var1);

    private native boolean n_GetItemDownloadInfo(long var1, long[] var3);

    private native long n_CreateQueryUGCDetailsRequest(long[] var1);

    private native SteamUGCDetails n_GetQueryUGCResult(long var1, int var3);

    private native long[] n_GetQueryUGCChildren(long var1, int var3);

    private native boolean n_ReleaseQueryUGCRequest(long var1);

    @Override
    public void onItemCreated(long itemID, boolean bUserNeedsToAcceptWorkshopLegalAgreement) {
        LuaEventManager.triggerEvent("OnSteamWorkshopItemCreated", SteamUtils.convertSteamIDToString(itemID), bUserNeedsToAcceptWorkshopLegalAgreement);
    }

    @Override
    public void onItemNotCreated(int result) {
        LuaEventManager.triggerEvent("OnSteamWorkshopItemNotCreated", result);
    }

    @Override
    public void onItemUpdated(boolean bUserNeedsToAcceptWorkshopLegalAgreement) {
        LuaEventManager.triggerEvent("OnSteamWorkshopItemUpdated", bUserNeedsToAcceptWorkshopLegalAgreement);
    }

    @Override
    public void onItemNotUpdated(int result) {
        LuaEventManager.triggerEvent("OnSteamWorkshopItemNotUpdated", result);
    }

    @Override
    public void onItemSubscribed(long itemID) {
        for (int _int = 0; _int < this.callbacks.size(); _int++) {
            ((ISteamWorkshopCallback)this.callbacks.get(_int)).onItemSubscribed(itemID);
        }
    }

    @Override
    public void onItemNotSubscribed(long itemID, int result) {
        for (int _int = 0; _int < this.callbacks.size(); _int++) {
            ((ISteamWorkshopCallback)this.callbacks.get(_int)).onItemNotSubscribed(itemID, result);
        }
    }

    @Override
    public void onItemDownloaded(long itemID) {
        for (int _int = 0; _int < this.callbacks.size(); _int++) {
            ((ISteamWorkshopCallback)this.callbacks.get(_int)).onItemDownloaded(itemID);
        }
    }

    @Override
    public void onItemNotDownloaded(long itemID, int result) {
        for (int _int = 0; _int < this.callbacks.size(); _int++) {
            ((ISteamWorkshopCallback)this.callbacks.get(_int)).onItemNotDownloaded(itemID, result);
        }
    }

    @Override
    public void onItemQueryCompleted(long handle, int numResults) {
        for (int _int = 0; _int < this.callbacks.size(); _int++) {
            ((ISteamWorkshopCallback)this.callbacks.get(_int)).onItemQueryCompleted(handle, numResults);
        }
    }

    @Override
    public void onItemQueryNotCompleted(long handle, int result) {
        for (int _int = 0; _int < this.callbacks.size(); _int++) {
            ((ISteamWorkshopCallback)this.callbacks.get(_int)).onItemQueryNotCompleted(handle, result);
        }
    }
}
